// activity/pages/activity_advert.js
var app = getApp();
var server = require('../../utils/server');
var common = require('../../utils/common');
var amapFile = require('../../libs/amap-wx.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isLocate:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      isLocate: app.globalData.isLocate
    })
    var that=this;
    if (app.globalData.city!=""){
    server.login(common.storePath + 'advert/getfirstpopbanner/', {
      "city": app.globalData.city
    }, function (res) {
      if (res.data.errno == "0") {
        let data = res.data.data;
        that.setData({
          land_url: data.land_url
        })
      } else {
        common.showErrorToast(res.data.errmsg);
      }
    })
    }else{
      var that=this;
      var gdMap = new amapFile.AMapWX({
        key: common.gdKey
      });
      gdMap.getRegeo({
        success: resp => {
          common.hideLoading();
          app.globalData.locateLongitude = resp[0].longitude;
          app.globalData.locateLatitude = resp[0].latitude;
          app.globalData.locateCity = resp[0].regeocodeData.addressComponent.city;
          app.globalData.locateProvince = resp[0].regeocodeData.addressComponent.province;
          if (app.globalData.longitude == null || app.globalData.latitude == null || app.globalData.location == null) {
            app.globalData.skipType = 1;
            app.globalData.longitude = resp[0].longitude;
            app.globalData.latitude = resp[0].latitude;
            app.globalData.location = resp[0].regeocodeData.pois[0] == undefined ? resp[0].name : resp[0].regeocodeData.pois[0].name;
            app.globalData.city = resp[0].regeocodeData.addressComponent.city;
          }
          server.login(common.storePath + 'advert/getfirstpopbanner/', {
            "city": app.globalData.city
          }, function (res) {
            if (res.data.errno == "0") {
              let data = res.data.data;
              that.setData({
                land_url: data.land_url
              })
            } else {
              common.showErrorToast(res.data.errmsg);
            }
          })
        },
        fail: info => {
          that.setData({
            isLocate: false
          })
          common.hideLoading();
          app.globalData.isLocate = false
        }
      })
    }
  }
})